// package-star/pages/brand-manager-add/index.js
import { PageReadonlyDataMixins, ManageTargetInfoUpdateMixins, UserInfoUpdateMixins } from '../../../behaviors/page';
import { navBack, tipToast } from '../../../common/env';
import { TipDialog } from '../../../common/tip';
import { createAdmin } from '../../../api/brd';
import { isValidPhone } from '../../../common/utils';

Page({
    mixins: [PageReadonlyDataMixins, ManageTargetInfoUpdateMixins, UserInfoUpdateMixins],
    data: {
        mobile: '',
    },
    onLoad(options) {
    },
    handleChange(e) {
        this.setData({
            mobile: e.detail,
        });
    },
    handleAdd() {
        const { mobile } = this.data;
        if (!isValidPhone(mobile)) {
            tipToast('请输入正确的手机号');
            return;
        } else if (mobile === this.data.userInfo.mobile) {
            tipToast('您已是管理员，无需添加');
            return;
        }
        TipDialog({
            title: '确定添加管理员？',
            ok() {
                createAdmin(mobile).then(flag => {
                    if (flag) {
                        navBack();
                    } else {
                        TipDialog({
                            title: '抱歉，未找到该用户',
                            message: `对方需要先进入${this.data.__readonly__.APPNAME}小程序并绑定手机号后，方可搜索添加对方为管理员。`,
                        });
                    }
                })
            },
        });
    },
})
